<?php
namespace app\controller;

use think\exception\ValidateException;
use app\validate\Software as SoftwareValidate;

class Software extends Base
{
    protected $middleware = ['checkAdmin' => [
        'except' => ['getNewVersion'],
    ]];

    protected function initialize()
    {
        parent::initialize();
        $this->logic = new \app\logic\Software;
    }

    /**
     * 列表分页数据
     */
    public function index()
    {
        if(request()->isAjax()) {
            $param = request()->param(['name', 'status', 'page', 'limit'], [], 'trim,strip_tags');
            // 验证数据
            try {
                validate(SoftwareValidate::class)->scene('search')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }
            return $this->jsonReturn($this->logic->getList($param));
        }
        return view();
    }

    /**
     * 新增软件
     */
    public function add()
    {
        if(request()->isAjax()) {
            $param = request()->param(['name', 'config', 'status'], [], 'trim,strip_tags');
            // 验证数据
            try {
                validate(SoftwareValidate::class)->scene('add')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->add($param));
        }
        return view();
    }

    /**
     * 修改软件
     */
    public function edit($id = 0)
    {
        if(request()->isAjax()) {
            $param = request()->param(['id', 'name', 'status'], [], 'trim,strip_tags');
            // 验证数据
            try {
                validate(SoftwareValidate::class)->scene('edit')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->edit($param));
        }

        return view("", [
            'info'  => $this->logic->getInfo(['id' => $id])
        ]);
    }

    /**
     * 删除软件
     */
    public function delete()
    {
        $param = request()->param(['ids'], [], 'trim,strip_tags');
        // 验证数据
        try {
            validate(SoftwareValidate::class)->scene('delete')->check($param);
        } catch (ValidateException $e) {
            return $this->jsonReturn(config('status.verify'), $e->getError());
        }

        return $this->jsonReturn($this->logic->delete($param['ids']));
    }

    /**
     * 设置状态
     */
    public function setStatus()
    {
        if(request()->isAjax()) {
            $id = request()->param('id', "", 'trim,intval');
            return $this->jsonReturn($this->logic->setStatus($id));
        }
    }

    /**
     * 获取最新版本配置
     */
    public function getNewVersion($token = "")
    {
        if(empty($token)) {
            return $this->jsonReturn(config("status.request"), "缺少参数");
        }

        return $this->logic->getNewVersion($token);
    }

    /**
     * 获取更新配置地址
     */
    public function getApiUrl()
    {
        if(request()->isAjax()) {
            $id = request()->param('id', "", 'trim,intval');

            return $this->jsonReturn($this->logic->getApiUrl($id));
        }
    }
}